﻿@{
    Layout = null;
}
@model ElingOA6_Admin_DSJCJ.Model.ItemPurchaseApply
<!DOCTYPE html>
@Html.Partial("LoadPanelPartial")
<html>
<head>
<title>用品采购申请</title>
      @if (false)
    {
        <script src="../../../../Scripts/jquery-vsdoc.js" type="text/javascript"></script>
        <script src="../../../../Scripts/ASPxScriptIntelliSense.js" type="text/javascript"></script>
    }
    <link href="@Url.Content("~/Content/Site.css", true)" rel="stylesheet" type="text/css" />

    <script src="@Url.Content("~/Scripts/jquery.min.js", true)" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/My97DatePicker/WdatePicker.js", true)" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/FloatDiv.js", true)" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/json2.js",true)" type="text/javascript"></script>

    <script src="@Url.Content("~/Common/GetRootPath.ashx", true)" type="text/javascript"></script>
    
    @Html.DevExpress().GetStyleSheets(

                new StyleSheet { ExtensionType = ExtensionType.GridView, Theme = DevExpressHelper.Theme }

)
    @Html.DevExpress().GetScripts(
                    new Script { ExtensionType = ExtensionType.ListBox }
    )
    <script type="text/javascript">

        var detailHtml= '{"FItemName":"","FItemSpec":"","FQty":0.0,"FPrice":0.0,"FAmount":0.0,"FRequestDate":null,"FNote":""}';


        $(document).ready(function () {
//            var type = $("#hdtype").val();
            var json = $("#hdjson").val();                       
            if(json != "")
            {
            var model = JSON.parse(json);
            var tableName = "additem";
            var array = "";            
                for (var i = 0; i < model.length; i++) {
                    array += i + ",";
                    $("#hditemNo").val(array);
                    var rowId = i;
                    var name = "name" + i;
                    var spec = "spce" + i;
                    var qty = "qty" + i;
                    var price = "price" + i;
                    var amount = "amount" + i;
                    var requestdate = "requestdate" + i;
                    var note = "note" + i;
                    $("#additem").append("<tr id='" + i + "' class='Add'><td align='center' class='tdClass'>" +
                    "<img src='../../../../Content/images/delete.gif' width='9' height='9' style='cursor:pointer' onclick=delRow('" + tableName + "','" + rowId + "') /></td>" +
                    "<td align='center' class='tdClass'>" + model[i]["FItemPurchaseApplyEntryID"] + "</td>" +
                    "<td align='center' class='tdClass'>"+
                    "<input name='" + name + "' id='" + name + "' type='text' value='" + model[i]["FItemName"] + "' style='width:100px' /></td>" +
                    "<td align='center' class='tdClass'><input name='" + spec + "' id='" + spec + "' type='text' value='" + model[i]["FItemSpec"] + "' style='width:100px' /></td>" +
                    "<td align='center' class='tdClass'><input name='" + qty + "' id='" + qty + "' type='text' value='" + model[i]["FQty"] + "' style='width:100px' onkeyup='qty()' /></td>" +
                    "<td align='center' class='tdClass'><input name='" + price + "' id='" + price + "' type='text' value='" + model[i]["FPrice"] + "' style='width:100px' onkeyup='price()' /></td>" +
                    "<td align='center' class='tdClass'><input name='" + amount + "' id='" + amount + "' type='text' value='" + model[i]["FAmount"] + "' style='width:100px' onkeyup=\"this.value=this.value.replace(/[^\\d]/g,'')\" onafterpaste=\"this.value=this.value.replace(/[^\\d]/g,'')\" /></td>" +
                    "<td align='center' class='tdClass'><input name='" + requestdate + "' id='" + requestdate + "' value='" + model[i]["StrFRequestDate"] + "' onclick='WdatePicker({readOnly:true})' class='Wdate' /> </td>" +
                    "<td align='center' class='tdClass'><input name='" + note + "' id='" + note + "' type='text' value='" + model[i]["FNote"] + "' style='width:200px' /></td></tr>");                       
                }
            }

//            if(type == "ADD"){
//                $("#EditChoose").hide();
//                $("#AddChoose").show();
//                $("#AddChoose1").hide();
//            }else if(type == "Edit"){
//                $("#EditChoose").show();
//                $("#AddChoose").hide();
//                 $("#AddChoose1").hide();               
//            }


        });

        function popDialog(itemNo){
            window.showModalDialog(rootPath + "/Admin/OfficeItemPurchaseApply/OfficeItemPurchaseApplyAddEntryInfo?itemNo=" + itemNo, 'popDialog', 'dialogWidth:500px;dialogHeight:220px');
            //window.location.href = rootPath + "/Admin/OfficeItemPurchaseApply/OfficeItemPurchaseApplyAddEntryInfo?itemNo=" + itemNo;
        }



        function ok() {

            var List = JSON.parse("[]");

            $('.Add').each(function () {
                var detailModel = JSON.parse(detailHtml);
                detailModel.FItemName = $.trim($(this).find('td').eq(2).find(':text').val());
                detailModel.FItemSpec = $.trim($(this).find('td').eq(3).find(':text').val());
                detailModel.FQty = $.trim($(this).find('td').eq(4).find(':text').val());
                detailModel.FPrice = $.trim($(this).find('td').eq(5).find(':text').val());
                detailModel.FAmount = $.trim($(this).find('td').eq(6).find(':text').val());
                detailModel.FRequestDate = $.trim($(this).find('td').eq(7).find(':text').val());
                detailModel.FNote = $.trim($(this).find('td').eq(8).find(':text').val());

                List.push(detailModel);

            });

            var ListStr = "";
            if (List.length > 0) {

                ListStr = JSON.stringify(List);
            }
                                            
            $("#hdresult").val(ListStr);                    
                                                          
            form1.submit();
            
        }

        function cancel() {
            window.location.href = rootPath + "/Admin/OfficeItemPurchaseApply/OfficeItemPurchaseApplyList";
        }


//        function itemChoose() {
//            var openUrl = rootPath + "/Admin/Dialog/OfficeItemManagerDialog"; //弹出窗口的url
//            var iWidth = 500; //弹出窗口的宽度;
//            var iHeight = 550; //弹出窗口的高度;
//            var iTop = (window.screen.availHeight - 30 - iHeight) / 2; //获得窗口的垂直位置;
//            var iLeft = (window.screen.availWidth - 10 - iWidth) / 2; //获得窗口的水平位置;
//            window.open(openUrl, "", "height=" + iHeight + ", width=" + iWidth + ", top=" + iTop + ", left=" + iLeft);
//        }

//        function Show(message) {      
//            $.ajax({
//                type: 'POST',
//                url: rootPath + '/Admin/OfficeItemPurchaseApply/OfficeItemPurchaseApplyAddEntry',
//                data: { message: message },
//                success: function (result) {
//                    var resultList = JSON.parse(result);
//                    $("#hdresult").val(result);
//                    var tableName = "additem";
//                    var array = "";
//                    for (var i = 0; i < resultList.length; i++) {                      
//                        array += resultList[i]["FItemNo"] + ",";
//                        $("#hditemNo").val(array);
//                        var rowId = resultList[i]["FItemNo"];
//                        var spec = "spce" + resultList[i]["FItemNo"];
//                        var qty = "qty" + resultList[i]["FItemNo"];
//                        var price = "price" + resultList[i]["FItemNo"];
//                        var amount = "amount" + resultList[i]["FItemNo"];
//                        var requestdate = "requestdate" + resultList[i]["FItemNo"];
//                        var note = "note" + resultList[i]["FItemNo"];
//                        var amountValue = resultList[i]["FPrice"];
//                        $("#additem").append("<tr id='" + resultList[i]["FItemNo"] + "'><td align='center' class='tdClass'>" +
//                        "<img src='../../../../Content/images/delete.gif' width='9' height='9' style='cursor:pointer' onclick=delRow('" + tableName + "','" + rowId + "') /></td>" +
//                        "<td align='center' class='tdClass'>待生成...</td>" +
//                        "<td align='center' class='tdClass'>"+
//                        "<a href='#' onclick='popDialog(\""+ resultList[i]["FItemNo"] +"\")'>" + resultList[i]["FItemName"] + "</a></td>" +
//                        "<td align='center' class='tdClass'><input name='" + spec + "' id='" + spec + "' type='text' value='" + resultList[i]["FItemSpec"] + "' style='width:100px' /></td>" +
//                        "<td align='center' class='tdClass'><input name='" + qty + "' id='" + qty + "' type='text' value='1' style='width:100px' onkeyup='qty()' /></td>" +
//                        "<td align='center' class='tdClass'><input name='" + price + "' id='" + price + "' type='text' value='" + resultList[i]["FPrice"] + "' style='width:100px' onkeyup='price()' /></td>" +
//                        "<td align='center' class='tdClass'><input name='" + amount + "' id='" + amount + "' type='text' value='"+amountValue+"' style='width:100px' /></td>" +
//                        "<td align='center' class='tdClass'><input name='" + requestdate + "' id='" + requestdate + "' value='@DateTime.Now.ToString("yyyy-MM-dd")' onclick='WdatePicker({readOnly:true})' class='Wdate' /> </td>" +
//                        "<td align='center' class='tdClass'><input name='" + note + "' id='" + note + "' type='text' value='' style='width:200px' /></td></tr>");
//                        
//                    }
//                }

//            });
//            
        //        }

        var a = 0;
        var array1 = "";   
        function addRowEntry() {
             a++;
             array1 += a + ",";
             $("#hditemNo").val(array1);
             var name = "name" + a;
             var spec = "spce" + a;
             var qty = "qty" + a;            
             var price = "price" + a;
             var amount = "amount" + a;
             var requestdate = "requestdate" + a;
             var note = "note" + a;                     
            $("#additem").append("<tr id='"+a+"' class='Add'><td align='center' class='tdClass'>" +
                        "<img src='../../../../Content/images/delete.gif' width='9' height='9' style='cursor:pointer' onclick=delRow('additem','"+a+"') /></td>" +
                        "<td align='center' class='tdClass'>待生成...</td>" +
                        "<td align='center' class='tdClass'>"+
                        "<input name='"+name+"' id='"+name+"' type='text' value='' style='width:100px' /></td>" +
                        "<td align='center' class='tdClass'><input name='"+spec+"' id='"+spec+"' type='text' value='' style='width:100px' /></td>" +
                        "<td align='center' class='tdClass'><input name='"+qty+"' id='"+qty+"' type='text' value='' style='width:100px' onkeyup='qty()' /></td>" +
                        "<td align='center' class='tdClass'><input name='"+price+"' id='"+price+"' type='text' value='' style='width:100px' onkeyup='price()'  /></td>" +
                        "<td align='center' class='tdClass'><input name='"+amount+"' id='"+amount+"' type='text' value='' style='width:100px' onkeyup=\"this.value=this.value.replace(/[^\\d]/g,'')\" onafterpaste=\"this.value=this.value.replace(/[^\\d]/g,'')\" /></td>" +
                        "<td align='center' class='tdClass'><input name='"+requestdate+"' id='"+requestdate+"' value='@DateTime.Now.ToString("yyyy-MM-dd")' onclick='WdatePicker({readOnly:true})' class='Wdate' /> </td>" +
                        "<td align='center' class='tdClass'><input name='"+note+"' id='"+note+"' type='text' value='' style='width:200px' /></td></tr>");
         }



        function qty() {
            var itemNo = $("#hditemNo").val().split(',');
            if(itemNo.length > 1 ){
                for (var i = 0; i < itemNo.length - 1; i++) {                
                    var qty = document.getElementById("qty" + itemNo[i]).value;
                    var price = document.getElementById("price" + itemNo[i]).value;
                    var amount = document.getElementById("amount" + itemNo[i]).value;
                    if (price == 0) {
                        if (qty == 0) {
                            document.getElementById("price" + itemNo[i]).value = 0;
                        } else {
                            document.getElementById("price" + itemNo[i]).value = amount / qty;
                        }
                    }
                    if (amount == 0) {
                        document.getElementById("amount" + itemNo[i]).value = qty * price;
                    }                

                    if (price == 0 && amount == 0) {
                        document.getElementById("price" + itemNo[i]).value = 0;
                        document.getElementById("amount" + itemNo[i]).value = 0;
                    }

                    if (price != 0 && amount != 0) {
                        document.getElementById("amount" + itemNo[i]).value = qty * price;
                     }
                              
                }
            }else{
                for (var i = 0; i < itemNo.length; i++) {                
                    var qty = document.getElementById("qty" + itemNo[i]).value;
                    var price = document.getElementById("price" + itemNo[i]).value;
                    var amount = document.getElementById("amount" + itemNo[i]).value;
                    if (price == 0) {
                        if (qty == 0) {
                            document.getElementById("price" + itemNo[i]).value = 0;
                        } else {
                            document.getElementById("price" + itemNo[i]).value = amount / qty;
                        }
                    }
                    if (amount == 0) {
                        document.getElementById("amount" + itemNo[i]).value = qty * price;
                    }                

                    if (price == 0 && amount == 0) {
                        document.getElementById("price" + itemNo[i]).value = 0;
                        document.getElementById("amount" + itemNo[i]).value = 0;
                    }

                    if (price != 0 && amount != 0) {
                        document.getElementById("amount" + itemNo[i]).value = qty * price;
                     }
                              
                }
            
            }
        }

        function price() {
            var itemNo = $("#hditemNo").val().split(',');
            if(itemNo.length > 1 ){
                for (var i = 0; i < itemNo.length - 1; i++) {
                    var qty = document.getElementById("qty" + itemNo[i]).value;
                    if (qty == 0) {
                        document.getElementById("price" + itemNo[i]).value = 0;
                        document.getElementById("amount" + itemNo[i]).value = 0;
                 
                    } else {
                        var price = document.getElementById("price" + itemNo[i]).value;
                        document.getElementById("amount" + itemNo[i]).value = qty * price;
                    }
                }
            }else
            {
                for (var i = 0; i < itemNo.length; i++) {
                    var qty = document.getElementById("qty" + itemNo[i]).value;
                    if (qty == 0) {
                        document.getElementById("price" + itemNo[i]).value = 0;
                        document.getElementById("amount" + itemNo[i]).value = 0;
                 
                    } else {
                        var price = document.getElementById("price" + itemNo[i]).value;
                        document.getElementById("amount" + itemNo[i]).value = qty * price;
                    }
                }
            
            }
         }



        function delRow(tableId, rowId){
             $("#" + tableId + " tr").remove("tr[id='" + rowId + "']");
            
        }

//        function itemEdit(){
//            $("#AddChoose1").show();
//            $("#EditChoose").hide();
//            $("#AddChoose").hide();

//             $("#additem").find("input").each(function(){
//                $(this).attr("disabled","disabled");
//             });

//        }

//        function itemEdit1(){
//            $("#AddChoose1").hide();
//            $("#EditChoose").show();
//            $("#AddChoose").hide();

//            $("#additem").find("input").each(function(){
//                $(this).removeAttr("disabled");
//             });

//        }


    </script>
</head>
<body>
@using (Html.BeginForm("Save", "OfficeItemPurchaseApply", FormMethod.Post, new { id = "form1" }))
{

     <table width="1000" align="center" class="tableForm" border="0">
            <tr>
                <td align="right" class="tdClass" width="10%">采购申请单号:</td>
                <td class="tdClass">
                    @Html.DevExpress().TextBox(settings =>
               {
                   settings.Name = "FItemPurchaseApplyID";
                   settings.Width = 200;
                   settings.Height = 20;
                   settings.Text = Model.FItemPurchaseApplyID == 0 ? "待生成..." : Model.FItemPurchaseApplyID.ToString();
                   settings.Enabled = false;
                   settings.Properties.ValidationSettings.EnableCustomValidation = true;
               }).GetHtml()
                </td>
                 <td align="right" class="tdClass">填报日期:</td>
                <td class="tdClass">
                    @Html.DevExpress().TextBox(settings =>
               {
                   settings.Name = "FApplyDate";
                   settings.Width = 200;
                   settings.Text = DateTime.Parse(Model.FApplyDate.ToString()).ToString("yyyy-MM-dd");
                   settings.Enabled = true;
                   settings.Properties.ValidationSettings.EnableCustomValidation = true;
               }).GetHtml()
                </td>
            </tr>
            <tr>
                <td align="right" class="tdClass">申请部门:</td>
                <td class="tdClass">
                   @Html.DevExpress().ComboBox(settings =>
              {
                  settings.Name = "FApplyDeptNo";
                  settings.Properties.DropDownStyle = DropDownStyle.DropDownList;
                  settings.Enabled = true;
                  settings.Width = 200;
                  settings.Height = 20;
                  settings.Properties.TextField = "Text";
                  settings.Properties.ValueField = "Value";
              }).BindList(ViewData["dept"] as List<SelectListItem>).Bind(Model.FApplyDeptNo).GetHtml()
                </td>
                 <td align="right" class="tdClass">申请人:</td>
                <td class="tdClass" >
                    @Html.DevExpress().ComboBox(settings =>
               {
                   settings.Name = "FApplyEmplNo";
                   settings.Properties.DropDownStyle = DropDownStyle.DropDownList;
                   settings.Enabled = true;
                   settings.Width = 200;
                   settings.Properties.TextField = "Text";
                   settings.Properties.ValueField = "Value";
               }).BindList(ViewData["empl"] as List<SelectListItem>).Bind(Model.FApplyEmplNo).GetHtml()
                </td>
            </tr>
             <tr>
                <td align="right" class="tdClass" >申请原因:</td>
                <td colspan="3" class="tdClass">
                    @Html.DevExpress().Memo(settings =>
               {
                   settings.Name = "FApplyReason";
                   settings.Width = System.Web.UI.WebControls.Unit.Percentage(60);
                   settings.Height = 60;
                   settings.Text = Model.FApplyReason;
                   settings.Enabled = true;
                   settings.Properties.Rows = 3;
                   settings.Properties.ValidationSettings.EnableCustomValidation = true;
               }).GetHtml()
                </td>
            </tr>
            <tr>
                <td colspan="4" class="tdClass">
                <table id="additem" width="600" align="center" border="0" class="tableForm">
                   <tr>
                        <td width="10%" class="tdClass" height="30" style =" text-align:center">
                        <img src="@Url.Content("~/Content/images/add.gif")" style="cursor:pointer"  onclick="addRowEntry()"/>
                        &nbsp;操作</td>
                        <td width="10%" class="tdClass" height="30" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "采购编号"; }).GetHtml()</td>
                        <td width="10%" class="tdClass" height="30" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "用品名称"; }).GetHtml()</td>
                        <td width="10%" class="tdClass" height="30" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "规格"; }).GetHtml()</td>  
                        <td width="10%" class="tdClass" height="30" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "数量"; }).GetHtml()</td>
                        <td width="10%" class="tdClass" height="30" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "单价"; }).GetHtml()</td>
                        <td width="10%" class="tdClass" height="30" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "金额"; }).GetHtml()</td>
                        <td width="10%" class="tdClass" height="30" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "要求日期"; }).GetHtml()</td> 
                        <td width="20%" class="tdClass" height="30" style =" text-align:center">@Html.DevExpress().Label(settings => { settings.Text = "备注"; }).GetHtml()</td>                   
                    </tr>   
                    

                </table>  
                    @*<div style="text-align:center" id="AddChoose">
                    @Html.DevExpress().Button(settings =>
               {
                   settings.Name = "btnItem";
                   settings.Text = "选择办公用品";
                   settings.ControlStyle.CssClass = "center";
                   //settings.Images.Image.Url = Url.Content("~/Content/ControlImages/item.png");
                   settings.ClientSideEvents.Click = "function(){itemChoose()}";
               }).GetHtml()
                        
                    </div>
                    <div style="text-align:center" id="EditChoose">
                    @Html.DevExpress().Button(settings =>
               {
                   settings.Name = "btnEditItem";
                   settings.Text = "新增明细";
                   settings.ControlStyle.CssClass = "center";
                   //settings.Images.Image.Url = Url.Content("~/Content/ControlImages/item.png");
                   settings.ClientSideEvents.Click = "function(){itemEdit()}";
               }).GetHtml()
                        
                    </div> 

                    <table style="text-align:center" id="AddChoose1" align="center">
                    <tr>
                    <td>
                    @Html.DevExpress().Button(settings =>
               {
                   settings.Name = "btnItem1";
                   settings.Text = "选择办公用品";
                   settings.ControlStyle.CssClass = "button";
                   //settings.Images.Image.Url = Url.Content("~/Content/ControlImages/item.png");
                   settings.ClientSideEvents.Click = "function(){itemChoose()}";
               }).GetHtml()
               </td><td>
                         @Html.DevExpress().Button(settings =>
               {
                   settings.Name = "btnEditItem1";
                   settings.Text = "编辑明细";
                   settings.ControlStyle.CssClass = "button";
                   //settings.Images.Image.Url = Url.Content("~/Content/ControlImages/item.png");
                   settings.ClientSideEvents.Click = "function(){itemEdit1()}";
               }).GetHtml()
               </td>
               </tr>
                    </table>*@
                         
                </td>
            </tr>
    </table>
    <table width="100%" class="tableForm">
            <tr>
            <td width="40%">&nbsp;</td>
                <td align="center">
                 @Html.DevExpress().Button(settings =>
            {
                settings.Name = "btnOK";
                settings.Text = "确定";
                settings.ControlStyle.CssClass = "button";
                settings.Images.Image.Url = Url.Content("~/Content/ControlImages/ok.png");
                settings.ClientSideEvents.Click = "function(){ok()}";
            }).GetHtml()

                @Html.DevExpress().Button(settings =>
           {
               settings.Name = "btnCancel";
               settings.Text = "取消";
               settings.ControlStyle.CssClass = "button";
               settings.Images.Image.Url = Url.Content("~/Content/ControlImages/no.png");
               settings.CausesValidation = false;
               settings.ClientSideEvents.Click = "cancel";
           }).GetHtml()

                </td>
            </tr>
        </table>
    
    @Html.Hidden("hditemNo")
    @Html.Hidden("hdresult")
   @Html.Hidden("hdID", ViewData["id"])
    @Html.Hidden("hdtype", ViewData["type"])  
     
    @Html.Hidden("hdjson", ViewData["json"])
    
                                         
}
     <script type="text/javascript">
         lp.Hide();
    </script>
</body>
</html>
